<?php

/**
 * This is the model class for table "xman_user_status".
 *
 * The followings are the available columns in table 'xman_user_status':
 * @property integer $user_status_user_id
 * @property integer $user_status_idle
 * @property string $user_status_free_period
 * @property integer $user_hour_per_day
 */
class UserStatus extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserStatus the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_user_status';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_status_user_id, user_status_idle, user_status_free_period, user_hour_per_day', 'required'),
			array('user_status_user_id, user_status_idle, user_hour_per_day', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_status_user_id, user_status_idle, user_status_free_period, user_hour_per_day', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user_status_user' => array(self::BELONGS_TO, 'User', 'user_status_user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_status_user_id' => 'User Status User',
			'user_status_idle' => 'User Status Idle',
			'user_status_free_period' => 'User Status Free Period',
			'user_hour_per_day' => 'User Hour Per Day',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		
		// Get search param
		$skill   = Yii::app()->request->getParam('skill');
		$level   = Yii::app()->request->getParam('level');
		$project = Yii::app()->request->getParam('project');

		$criteria=new CDbCriteria;

		$criteria->compare('user_status_user_id',$this->user_status_user_id);

		$criteria->compare('user_status_idle',$this->user_status_idle);

		$criteria->compare('user_status_free_period',$this->user_status_free_period,true);

		$criteria->compare('user_hour_per_day',$this->user_hour_per_day);
		
		// Join with other table
		if ($skill && $level){
			$criteria->join = "
				LEFT JOIN 
					xman_user_skill	as us on us.user_skill_user_id = user_status_user_id
				
			";
			$criteria->compare('us.user_skill_skill_id', $skill);
			$criteria->compare('us.skill_level', $level);
		}
		
		if ($project){
			$criteria->join .= "
				LEFT JOIN
					xman_project_user as pu on pu.project_user_user_id = user_status_user_id
			";
			$criteria->condition .= "and (pu.project_user_project_id = $project)";
		}
		return new CActiveDataProvider('UserStatus', array(
			'criteria'=>$criteria,
			'pagination'=>array(
        		'pageSize'=>3,
    		)
		));
	}
	
	/**
	 * @return status of user
	 */
	
	public static function getStatus($status) {
		if ($status == 0)
			return 'Bận';
		return 'Rảnh';
	}
	
	/**
	 * @return list status
	 */
	public static function getListStatus() {
		return array(
			NULL=>'--',
			0 => 'Bận',
			1 => 'Rảnh',
		);
	}
}